#include "ZigbeeDevice.h"
#include "Protocol/CloudProtocol.h"
#include "File/Xml/BasicXml.h"
ZigbeeDevice::ZigbeeDevice(QObject *parent) :
    BaseInterface<ZigbeeDevice>(parent)
{
     qDebug()<<"ZigbeeDevice running-1";


    m_pInstance = this;
    pointProtocol = new PointProtocol(this);
    zigbeeConfigProtocol = new ZigbeeConfigProtocol(this);
    zigbeeControlProtocol = new ZigbeeControlProtocol(this);
    zigbeeNewControlProtocol = new ZigbeeNewControlProtocol(this);

     m_rebootTimerCount=0;
    BasicXml *basicXml = BasicXml::getInstance();
     if(basicXml->rebootIntervalTIme() > 0)
     {
         notifyRebootTimer = new QTimer(this);
         connect(notifyRebootTimer, SIGNAL(timeout()), this, SLOT(NotifyRebootSlot()));
         notifyRebootTimer->setInterval(1000);
         notifyRebootTimer ->start();
         m_isFirstNotify=1;
     }

    qDebug()<<"zigbee running";
    this->registerProtocol(pointProtocol);
    this->registerProtocol(zigbeeConfigProtocol);
    this->registerProtocol(zigbeeControlProtocol);
    this->registerProtocol(zigbeeNewControlProtocol);

    registerCmdHandler(WinSetZigbeeParamCmd::FunctionCode,WinSetZigbeeParamCmd::staticMetaObject, &ZigbeeDevice::winSetZigbeeParamCmdHandle);
    registerCmdHandler(SetZigbeeParamCmd::FunctionCode,SetZigbeeParamCmd::staticMetaObject, &ZigbeeDevice::setZigbeeParamCmdHandle);
    registerCmdHandler(WinGetZigbeeParamCmd::FunctionCode,WinGetZigbeeParamCmd::staticMetaObject, &ZigbeeDevice::winGetZigbeeParamCmdHandle);
    registerCmdHandler(GetZigbeeParamCmd::FunctionCode,GetZigbeeParamCmd::staticMetaObject, &ZigbeeDevice::getZigbeeParamCmdHandle);
    registerCmdHandler(WinRestartZigbeeCmd::FunctionCode,WinRestartZigbeeCmd::staticMetaObject, &ZigbeeDevice::winRestartZigbeeCmdHandle);
    registerCmdHandler(RestartZigbeeCmd::FunctionCode,RestartZigbeeCmd::staticMetaObject, &ZigbeeDevice::restartZigbeeCmdHandle);

    registerCmdHandler(WinStartNewZigbeeNetCmd::FunctionCode,WinStartNewZigbeeNetCmd::staticMetaObject, &ZigbeeDevice::winStartNewZigbeeCmdHandle);
    registerCmdHandler(WinSetAllowJoinCmd::FunctionCode,WinSetAllowJoinCmd::staticMetaObject, &ZigbeeDevice::winSetAllowJoinCmdHandle);
    registerCmdHandler(WinSetBanJoinCmd::FunctionCode,WinSetBanJoinCmd::staticMetaObject, &ZigbeeDevice::winSetBanJoinCmdHandle);
    registerCmdHandler(WinSetQueryNewZigbeeParamCmd::FunctionCode,WinSetQueryNewZigbeeParamCmd::staticMetaObject, &ZigbeeDevice::winQueryNewZigbeeParamCmdHandle);
    registerCmdHandler(WinSetQueryZigbeeVersionCmd::FunctionCode,WinSetQueryZigbeeVersionCmd::staticMetaObject, &ZigbeeDevice::winQueryZigbeeVersionCmdHandle);
 //   registerCmdHandler(Zigbee3NewJoinParamCmd::FunctionCode,Zigbee3NewJoinParamCmd::staticMetaObject, &ZigbeeDevice::Zigbee3NewJoinParamCmdHandle);
 //   registerCmdHandler(Zigbee3QueryBackCmd::FunctionCode,Zigbee3QueryBackCmd::staticMetaObject, &ZigbeeDevice::zigbee3QueryBackData);

    registerCmdHandler(EquipmentIntoNetCmd::FunctionCode,EquipmentIntoNetCmd::staticMetaObject, &ZigbeeDevice::equipmentIntoNetCmdHandle);
  //  registerCmdHandler(EquipmentIntoNetCmd::FunctionCode,EquipmentIntoNetCmd::staticMetaObject, &ZigbeeDevice::ZigBeeDeliverCmdHandle);

    registerCmdHandler(ReceiveControlPointListCmd::FunctionCode,ReceiveControlPointListCmd::staticMetaObject, &ZigbeeDevice::receiveControlPointListCmdHandle);

//   registerCmdHandler(ZigBeeDeliverCmd::FunctionCode,ZigBeeDeliverCmd::staticMetaObject, &ZigbeeDevice::ZigBeeDeliverCmdHandle);

    registerFuncHandler(ControlPointFunc::FunctionCode, &ZigbeeDevice::controlPointFuncHandle);
    qDebug()<<"ZigbeeDevice running-2";
}

void ZigbeeDevice::winSetZigbeeParamCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    WinSetZigbeeParamCmd *winSetZigbeeParamCmd = qobject_cast<WinSetZigbeeParamCmd*>(sharedCmd.data());

    quint8 type = winSetZigbeeParamCmd->type();
    quint8 channel = winSetZigbeeParamCmd->channel();
    quint8 power = winSetZigbeeParamCmd->power();
    QByteArray address = winSetZigbeeParamCmd->address();
    QByteArray panId = winSetZigbeeParamCmd->panId();
    QByteArray extPanId = winSetZigbeeParamCmd->extPanId();
    QByteArray nwkKey = winSetZigbeeParamCmd->nwkKey();

    SetZigbeeParamCmd *setZigbeeParamCmd = new SetZigbeeParamCmd();
    setZigbeeParamCmd->setType(type);
    setZigbeeParamCmd->setChannel(channel);
    setZigbeeParamCmd->setPower(power);
    setZigbeeParamCmd->setAddress(address);
    setZigbeeParamCmd->setPanId(panId);
    setZigbeeParamCmd->setExtPanId(extPanId);
    setZigbeeParamCmd->setNwkKey(nwkKey);

    QSharedPointer<BaseCmd> sharedSetZigbeeParamCmd(setZigbeeParamCmd);
    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedSetZigbeeParamCmd);
}

void ZigbeeDevice::setZigbeeParamCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    SetZigbeeParamCmd *setZigbeeParamCmd = qobject_cast<SetZigbeeParamCmd*>(sharedCmd.data());
    bool isSucceed = setZigbeeParamCmd->isSucceed();

    WinSetZigbeeParamCmd *winSetZigbeeParamCmd = new WinSetZigbeeParamCmd();
    winSetZigbeeParamCmd->setIsSucceed(isSucceed);

    QSharedPointer<BaseCmd> sharedWinSetZigbeeParamCmd(winSetZigbeeParamCmd);
    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedWinSetZigbeeParamCmd);

}

void ZigbeeDevice::winGetZigbeeParamCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);

    GetZigbeeParamCmd *getZigbeeParamCmd = new GetZigbeeParamCmd();
    QSharedPointer<BaseCmd> sharedGetZigbeeParamCmd(getZigbeeParamCmd);
    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedGetZigbeeParamCmd);
}

void ZigbeeDevice::getZigbeeParamCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    GetZigbeeParamCmd *getZigbeeParamCmd = qobject_cast<GetZigbeeParamCmd*>(sharedCmd.data());

    quint8 type = getZigbeeParamCmd->type();
    quint8 channel = getZigbeeParamCmd->channel();
    quint8 power = getZigbeeParamCmd->power();
    QByteArray address = getZigbeeParamCmd->address();
    QByteArray panId = getZigbeeParamCmd->panId();
    QByteArray extPanId = getZigbeeParamCmd->extPanId();
    QByteArray nwkKey = getZigbeeParamCmd->nwkKey();

    WinGetZigbeeParamCmd *winGetZigbeeParamCmd = new WinGetZigbeeParamCmd();
    winGetZigbeeParamCmd->setType(type);
    winGetZigbeeParamCmd->setChannel(channel);
    winGetZigbeeParamCmd->setPower(power);
    winGetZigbeeParamCmd->setAddress(address);
    winGetZigbeeParamCmd->setPanId(panId);
    winGetZigbeeParamCmd->setExtPanId(extPanId);
    winGetZigbeeParamCmd->setNwkKey(nwkKey);

    QSharedPointer<BaseCmd> sharedWinGetZigbeeParamCmd(winGetZigbeeParamCmd);
    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedWinGetZigbeeParamCmd);
}

void ZigbeeDevice::winRestartZigbeeCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);

    RestartZigbeeCmd *restartZigbeeCmd = new RestartZigbeeCmd();
    QSharedPointer<BaseCmd> sharedRestartZigbeeCmd(restartZigbeeCmd);
    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedRestartZigbeeCmd);
}

void ZigbeeDevice::restartZigbeeCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    RestartZigbeeCmd *restartZigbeeCmd = qobject_cast<RestartZigbeeCmd*>(sharedCmd.data());
    bool isSucceed = restartZigbeeCmd->isSucceed();

    WinRestartZigbeeCmd *winRestartZigbeeCmd = new WinRestartZigbeeCmd();
    winRestartZigbeeCmd->setIsSucceed(isSucceed);

    QSharedPointer<BaseCmd> sharedWinRestartZigbeeCmd(winRestartZigbeeCmd);
    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedWinRestartZigbeeCmd);
}

void ZigbeeDevice::equipmentIntoNetCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    qDebug()<<"hahhahahaha";
    EquipmentIntoNetCmd *equipmentIntoNetCmd = qobject_cast<EquipmentIntoNetCmd*>(sharedCmd.data());
    quint8 index = equipmentIntoNetCmd->index();
    ZigbeeStruct zigbee = equipmentIntoNetCmd->zigbee();

    ZigbeeListXml *zigbeeListXml = ZigbeeListXml::getInstance();
    zigbeeListXml->modify(index,zigbee);

}

void ZigbeeDevice::receiveControlPointListCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    qDebug()<<"~~~~ZigbeeDevice::receiveControlPointListCmdHandle";
    ReceiveControlPointListCmd *receiveControlPointListCmd = qobject_cast<ReceiveControlPointListCmd*>(sharedCmd.data());
    QList<ControlPointStruct> controlPointList = receiveControlPointListCmd->controlPointList();

    ControlPointFunc *pointControlFunc = new ControlPointFunc();
    pointControlFunc->setSourceDevice(this->name());
    pointControlFunc->setSourcePointList(controlPointList);

    QSharedPointer<BaseFunc> func(pointControlFunc);
    emit transferFuncSignal(func);
}

void ZigbeeDevice::winStartNewZigbeeCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);

    CreataNewZigbeeCmd *creataNewZigbeeCmd = new CreataNewZigbeeCmd();
    ResNewZigbeeCmd *resNewZigbeeCma=new ResNewZigbeeCmd();
    RestartZigbeeCmd *restartZigbeeCmd=new RestartZigbeeCmd();
    QSharedPointer<BaseCmd> sharedcreataNewZigbeeCmd(creataNewZigbeeCmd);
    QSharedPointer<BaseCmd> sharedresNewZigbeeCma(resNewZigbeeCma);
    QSharedPointer<BaseCmd> sharedrestartZigbeeCmd(restartZigbeeCmd);
    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedresNewZigbeeCma);

    this->sendCmd(target,sharedcreataNewZigbeeCmd);
//this->sendCmd(target,sharedrestartZigbeeCmd);
}

void ZigbeeDevice::winSetAllowJoinCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);

    AllowNewJoinCmd *allowNewJoinCmd = new AllowNewJoinCmd();

    QSharedPointer<BaseCmd> sharedallowNewJoinCmd(allowNewJoinCmd);
    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedallowNewJoinCmd);

}

void ZigbeeDevice::winSetBanJoinCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);

    BanNewJoinCmd *banNewJoinCmd = new BanNewJoinCmd();

    QSharedPointer<BaseCmd> sharedbanNewJoinCmd(banNewJoinCmd);

    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedbanNewJoinCmd);

}

void ZigbeeDevice::winQueryNewZigbeeParamCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);

    QueryNewZigbeeParamCmd *queryNewZigbeeParamCmd = new QueryNewZigbeeParamCmd();

    QSharedPointer<BaseCmd> sharedqueryNewZigbeeParamCmd(queryNewZigbeeParamCmd);

    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedqueryNewZigbeeParamCmd);

}

void ZigbeeDevice::winQueryZigbeeVersionCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);

    QueryZigbeeVersionCmd *queryZigbeeVersionCmd = new QueryZigbeeVersionCmd();

    QSharedPointer<BaseCmd> sharedqueryNewZigbeeParamCmd(queryZigbeeVersionCmd);

    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedqueryNewZigbeeParamCmd);

}




void ZigbeeDevice::Zigbee3NewJoinParamCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Zigbee3NewJoinParamCmd *setControlPointCmd = qobject_cast<Zigbee3NewJoinParamCmd*>(sharedCmd.data());
    Zigbee3Struct zigbeeStruck;
    Zigbee3ListXml *zigbeeListXml = Zigbee3ListXml::getInstance();

    zigbeeStruck.setMac(setControlPointCmd->Mac());
    zigbeeStruck.setNwk(setControlPointCmd->Nwk());
    QByteArray data;
    data.clear();
    data.append(setControlPointCmd->Nwk().data());
    data.append(setControlPointCmd->Mac().data());



    zigbeeListXml->modify(1,zigbeeStruck);
    PortStruct target;
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
         {
             target = port->portParam();
         }
    }
    emit sendDataSignal(target,data);

    qDebug()<<"join zigbee3 now";
}

void ZigbeeDevice::ZigBeeDeliverCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);
     qDebug()<<"now deliver is coming";
//    ZigBeeDeliverCmd *zigBeeDeliverCmd = qobject_cast<ZigBeeDeliverCmd*>(sharedCmd.data());
//    CloudProtocol cloudProtocol;
//    QList<Package>packagelist;
//    packagelist=cloudProtocol.parseData(zigBeeDeliverCmd->getCmd());
//    foreach(Package package,packagelist)
//    {
//        int functionCode = package.function();
//        CmdHandlePointer pHandle = m_cmdHandleMap.value(functionCode,NULL);
//        qDebug()<<"function="<<functionCode;
//        qDebug()<<"pHandle is null?";
//        if(pHandle != NULL)
//        {
//            qDebug()<<"pHandle is not null";
//       //     (m_pInstance->*pHandle)(source,sharedCmd);
//        }
//    }

}



void ZigbeeDevice::controlPointFuncHandle(QSharedPointer<BaseFunc> sharedFunc)
{
    ControlPointFunc *pointControlFunc = qobject_cast<ControlPointFunc*>(sharedFunc.data());
    QList<ControlPointStruct> funcList=pointControlFunc->controlPointList();
    QList<ControlPointStruct> controlPointList;

    qDebug()<<"zigbee controlpointhandle!"<<pointControlFunc->sourceDevice()<<" "<<this->name();
    if(pointControlFunc->sourceDevice() == this->name())
    {
        qDebug()<<"This zigbee name!";
        qDebug()<<"controlPointList="<<QString::number(funcList.size());
        qDebug()<<"sourcePointList="<<QString::number(pointControlFunc->sourcePointList().size());

        foreach(ControlPointStruct Struct , pointControlFunc->sourcePointList())
        {
        SavePointXml *savePointXml =SavePointXml::getInstance();
        quint16 address = Struct.address();
        quint16 value = Struct.value();
        savePointXml->SavePointData(address,value);
        QString stata="Read-Zigbee";
        savePointXml->JoinSystemList(address,value,stata);
        }
         controlPointList.append(pointControlFunc->controlPointList());
      /*  foreach(ControlPointStruct Struct , pointControlFunc->controlPointList())
        //foreach(ControlPointStruct Struct , pointControlFunc->sourcePointList())
        {
            bool SendZigbee=false;
            foreach(QString portname,Struct.Portlist())
            {
                qDebug()<<"Portname="<<portname;
                if(portname=="ZIGBEE")
                {
                     qDebug()<<"truezigbee";
                   SendZigbee=true;
                }
            }
            if(Struct.Portlist().size()==0||SendZigbee==true)
            {
                qDebug()<<"tryjoin Read-Zigbee";


                if(Struct.delayTime()==0)
                {
                    qDebug()<<"joinlist="<<QString::number(Struct.address());
                 controlPointList.append(Struct);
                }else
                {
                    qDebug()<<"ZigbeeDevice joinDelayTime22---"<<Struct.address()<<" "<<Struct.value();
                  //  controlPoint.setPortStruct(target);
                    SceneListXml *sceneListXml = SceneListXml::getInstance();
                   sceneListXml->joinDelayTimeList(Struct);
                }
            }
        }*/
    }
    else
    {
//        controlPointList.append(pointControlFunc->sourcePointList());
//        controlPointList.append(pointControlFunc->controlPointList());
        qDebug()<<"zigbeedevice no this name";
        qDebug()<<"controlPointList="<<QString::number(funcList.size());
        qDebug()<<"controlPointList="<<QString::number(pointControlFunc->sourcePointList().size());
        foreach(ControlPointStruct Struct , pointControlFunc->sourcePointList())
        {
            bool SendZigbee=false;
            foreach(QString portname,Struct.Portlist())
            {
                qDebug()<<"Portname="<<portname;
                if(portname=="ZIGBEE")
                {
                     qDebug()<<"truezigbee";
                   SendZigbee=true;
                }
            }
            if(Struct.Portlist().size()==0||SendZigbee==true)
            {
                qDebug()<<"tryjoin";
                if(Struct.delayTime()==0)
                {
                 qDebug()<<"joinlist="<<QString::number(Struct.address());
                 controlPointList.append(Struct);
                }else
                {
                  //  qDebug()<<"ZigbeeDevice joinDelayTime---"<<Struct.address()<<" "<<Struct.value();

                  //  SceneListXml *sceneListXml = SceneListXml::getInstance();
                  // sceneListXml->joinDelayTimeList(Struct);
                }
            }
        }
        foreach(ControlPointStruct Struct , pointControlFunc->controlPointList())
        {
            bool SendZigbee=false;
            foreach(QString portname,Struct.Portlist())
            {
                qDebug()<<"Portname="<<portname;
                if(portname=="ZIGBEE")
                {
                    qDebug()<<"truezigbee";
                   SendZigbee=true;
                }
            }
            if(Struct.Portlist().size()==0||SendZigbee==true)
            {
                qDebug()<<"tryjoin";
              if(Struct.delayTime()==0)
              {
              qDebug()<<"joinlist="<<QString::number(Struct.address());
               controlPointList.append(Struct);
              }else
              {
                 // qDebug()<<"ZigbeeDevice joinDelayTime11---"<<Struct.address()<<" "<<Struct.value();
                //  controlPoint.setPortStruct(target);
                 // SceneListXml *sceneListXml = SceneListXml::getInstance();
               //  sceneListXml->joinDelayTimeList(Struct);
              }
            }
        }
    }
    if(controlPointList.isEmpty())
    {
        return;
    }
     qDebug()<<"List is no Empty---";
    ZigbeeListXml *zigbeeListXml = ZigbeeListXml::getInstance();
    Zigbee3ListXml *zigbee3ListXml = Zigbee3ListXml::getInstance();
    QList<ZigbeeStruct> zigbeeList = zigbeeListXml->zigbeeList(controlPointList);
    if((zigbee3ListXml->ZigbeeVersion().toHex().toInt(0,16))!=0x05)
    {
        foreach(const ZigbeeStruct &zigbee,zigbeeList)
        {
        SendControlPointListCmd *sendControlPointListCmd = new SendControlPointListCmd();
        sendControlPointListCmd->setZigbee(zigbee);
        QSharedPointer<BaseCmd> sharedSendControlPointListCmd(sendControlPointListCmd);
        PortStruct target;
        foreach (const BasePort *port , m_portList)
        {
             if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
             {
                 target = port->portParam();
             }
        }
        unsigned int mesc=1000;
        QTime Timer=QTime::currentTime().addMSecs(mesc);
        while(QTime::currentTime()<Timer);
        sendCmd(target,sharedSendControlPointListCmd);
        }
      }

            if((zigbee3ListXml->ZigbeeVersion().toHex().toInt(0,16))==0x05)
            {
                qDebug()<<"Version=5";
                int maxsize=15;
                if(controlPointList.size()>maxsize)
                {
                    qDebug()<<"size>30";
                    int Sendnum=controlPointList.size()/maxsize;
                    for(int i=0;i<Sendnum;i++)
                    {
                        AiringControlPointListCmd *airingControlPointListCmd = new AiringControlPointListCmd();
                        airingControlPointListCmd->setControlList(controlPointList.mid(i*maxsize,maxsize));

                        QSharedPointer<BaseCmd> sharedSendControlPointListCmd(airingControlPointListCmd);
                        PortStruct target;
                        foreach (const BasePort *port , m_portList)
                        {
                          if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
                             {
                               target = port->portParam();
                             }
                        }
                        unsigned int mesc=1000;
                        QTime Timer=QTime::currentTime().addMSecs(mesc);
                        while(QTime::currentTime()<Timer);
                        sendCmd(target,sharedSendControlPointListCmd);
                        qDebug()<<"sendzigbee i="<<QString::number(i);
                    }
                    if(controlPointList.size()-(Sendnum*maxsize)>0)
                    {
                        AiringControlPointListCmd *airingControlPointListCmd = new AiringControlPointListCmd();
                        airingControlPointListCmd->setControlList(controlPointList.mid(Sendnum*maxsize,controlPointList.size()-(Sendnum*maxsize)));

                        QSharedPointer<BaseCmd> sharedSendControlPointListCmd(airingControlPointListCmd);
                        PortStruct target;
                        foreach (const BasePort *port , m_portList)
                        {
                          if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
                             {
                               target = port->portParam();
                             }
                        }
                        unsigned int mesc=1000;
                        QTime Timer=QTime::currentTime().addMSecs(mesc);
                        while(QTime::currentTime()<Timer);
                        sendCmd(target,sharedSendControlPointListCmd);
                        qDebug()<<"sendzigbee";
                    }
                }
                else
                {
                AiringControlPointListCmd *airingControlPointListCmd = new AiringControlPointListCmd();
                airingControlPointListCmd->setControlList(controlPointList);

                QSharedPointer<BaseCmd> sharedSendControlPointListCmd(airingControlPointListCmd);
                PortStruct target;
                foreach (const BasePort *port , m_portList)
                {
                  if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
                     {
                       target = port->portParam();
                     }
                }
                unsigned int mesc=1000;
                QTime Timer=QTime::currentTime().addMSecs(mesc);
                while(QTime::currentTime()<Timer);
                sendCmd(target,sharedSendControlPointListCmd);
                qDebug()<<"sendzigbee";
                }
            }


}

void ZigbeeDevice::NotifyRebootSlot()
{

    //Package package;
   // QByteArray pointDeviceData;
    bool ok;
    QList<ControlPointStruct> controlPointList;
    QString curdate = QDate::currentDate().toString("yyyy-MM-dd");
    QString year = curdate.mid(0,4);
    QString month = curdate.mid(5,2);
    QString date = curdate.mid(8,2);
    QString curtime = QTime::currentTime().toString("hh:mm:ss");
    QString hour = curtime.mid(0,2);
    QString min = curtime.mid(3,2);
    QString sec = curtime.mid(6,2);
    PortStruct target;
    if(sec == "00")
    {
     qDebug()<<"[ZigbeeDevice::NotifyRebootSlot]-"<<curdate<<" year:"<<year<<"month:"<<month<<"date:"<<date;
     qDebug()<<"[ZigbeeDevice::NotifyRebootSlot]-"<<curtime<<" hour:"<<hour<<"min:"<<min<<"sec:"<<sec;

    }

    m_rebootTimerCount++;

    if(year.toInt(&ok,10)>=2024)
    {
         BasicXml *basicXml = BasicXml::getInstance();
    // if(m_isFirstNotify == 1)
         if(sec == "00")
         qDebug()<<"[ZigbeeDevice::NotifyRebootSlot]-m_rebootTimerCount "<<QString::number(m_rebootTimerCount,10)<<"--"<<QString::number(basicXml->rebootIntervalTIme()*1440*60,10)<<"--m_isFirstNotify-"<<QString::number(m_isFirstNotify,10);
     if((((hour.toInt()==basicXml->rebootTimePoint())&&(min=="00")) && ((sec=="00")||(sec=="03")))&&((m_rebootTimerCount >= basicXml->rebootIntervalTIme()*1440*60)||(m_isFirstNotify==1)))
     {
         AiringControlPointListCmd *airingControlPointListCmd = new AiringControlPointListCmd();
         ControlPointStruct cp;
         cp.setAddress(65534);
         cp.setValue(65534);
         controlPointList.append(cp);
         airingControlPointListCmd->setControlList(controlPointList);

         QSharedPointer<BaseCmd> sharedSendControlPointListCmd(airingControlPointListCmd);
         PortStruct target;
         foreach (const BasePort *port , m_portList)
         {
           if((port->portParam().type() == "UART" && port->portParam().name() == "Zigbee"))
              {
                target = port->portParam();
              }
         }
         unsigned int mesc=1000;
         QTime Timer=QTime::currentTime().addMSecs(mesc);
         while(QTime::currentTime()<Timer);
         sendCmd(target,sharedSendControlPointListCmd);
         qDebug()<<"sendzigbee restart";
     }
    }

}


